﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace jnxc.Web.farmRecord
{
    /// <summary>
    /// GetFarm 的摘要说明
    /// </summary>
    public class GetFarm : IHttpHandler, IRequiresSessionState
    {
        jnxc.BLL.farmRecord bll = new jnxc.BLL.farmRecord();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "dis")
                Querydis(context);
            else if (ac == "tt")
                QueryFileTX(context);
            else if (ac == "delete")
                delete(context);
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {

            
                int uid = 0;
                string uidstr = context.Request.QueryString["farmId"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                if (bll.Delete(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
        }
        public void Querydis(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int level = Web.Admin.myinfo.getLevel(context);
                string uprovince, ucity, ucounty;
                string StartTime = "", EndTime = "";
                uprovince = ucity = ucounty = "";
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                if (null != context.Request.Form["uprovince"])
                {
                    uprovince = context.Request.Form["uprovince"].ToString().Trim();
                }
                if (null != context.Request.Form["ucity"])
                {
                    ucity = context.Request.Form["ucity"].ToString().Trim();
                }
                if (null != context.Request.Form["ucounty"])
                {
                    ucounty = context.Request.Form["ucounty"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (level <= 3)
                    uprovince = pcc[0];
                if (level <= 2)
                {
                    ucity = pcc[1];
                }
                if (level <= 1)
                {
                    ucounty = pcc[2];
                }
                if (ucounty != "")
                {
                    strWhere.AppendFormat(" coname='{0}' and ", ucounty);
                }

                if (ucity != "")
                {
                    strWhere.AppendFormat(" cname='{0}' and ", ucity);
                }

                if (uprovince != "")
                {
                    strWhere.AppendFormat(" pname='{0}' and ", uprovince);

                }
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" investigationTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" investigationTime<='{0}' and ", EndTime);
                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPagedis(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCountdis(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string tname, tbh, tcbh;
                tname = tbh = tcbh = "";
                if (null != context.Request.QueryString["tn"])
                {
                    tname = context.Request.QueryString["tn"].ToString().Trim();
                }
                if (null != context.Request.QueryString["tbh"])
                {
                    tbh = context.Request.QueryString["tbh"].ToString().Trim();
                }
                if (null != context.Request.QueryString["tcdname"])
                {
                    tcbh = context.Request.QueryString["tcdname"].ToString().Trim();
                }

                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (tbh != "")
                {
                    strWhere.AppendFormat(" tbh='{0}' and ", tbh);
                }
                if (tcbh != "")
                {
                    strWhere.AppendFormat(" tcdid={0} and ", tcbh);
                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void QueryFileTX(HttpContext context)
        {

            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string tname, tbh, tcbh;
                tname = tbh = tcbh = "";
                if (null != context.Request.QueryString["tn"])
                {
                    tname = context.Request.QueryString["tn"].ToString().Trim();
                }
                if (null != context.Request.QueryString["tbh"])
                {
                    tbh = context.Request.QueryString["tbh"].ToString().Trim();
                }
                if (null != context.Request.QueryString["tcdname"])
                {
                    tcbh = context.Request.QueryString["tcdname"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (tbh != "")
                {
                    strWhere.AppendFormat(" tbh='{0}' and ", tbh);
                }
                if (tcbh != "")
                {
                    strWhere.AppendFormat(" tcdid={0} and ", tcbh);
                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                DataSet ds = bll.GetListV(strWhere.ToString());
                if (tbh == "") tbh = DateTime.Now.Ticks.ToString();
                OutWord.outwordtf(context.Server.MapPath("~/word/tfarm.doc"), tbh + ".doc", context, ds);
            }

        }       
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}